package main

import (
	"errors"
	"fmt"
)

func divide(a, b int) (int, error) {
	if b == 0 {
		return 0, errors.New("divide by zero")
	}
	return a / b, nil
}

func risky() {
	panic("出错")
}

func safeCall() {
	defer func() {
		r := recover()
		if r != nil {
			fmt.Println("捕获 panic: ", r)
		}
	}()
	risky()
}

func main() {
	result, err := divide(10, 0)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(result)
	}

	//risky()
	safeCall()
	err1 := fmt.Errorf("无效")
	fmt.Println(err1)

}
